package com.tsingteng.cosfun.ui.costar.drafts;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiniu.pili.droid.shortvideo.PLDraft;
import com.qiniu.pili.droid.shortvideo.PLDraftBox;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BaseFragment;
import com.tsingteng.cosfun.bean.DBBean;
import com.tsingteng.cosfun.bean.DBPlayBean;
import com.tsingteng.cosfun.ui.message.CommonActivity;
import com.tsingteng.cosfun.ui.message.addplay.WritingPlayContract;
import com.tsingteng.cosfun.ui.message.addplay.WritingPlayFragment;
import com.tsingteng.cosfun.utils.DBUtils;
import com.tsingteng.cosfun.utils.DataUtil;
import com.tsingteng.cosfun.utils.DataUtils;
import com.tsingteng.cosfun.utils.TimeUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.PressImageView;
import com.tsingteng.cosfun.widget.dialog.ContentDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 描述 :便当草稿箱
 * 创建人: 肖利.
 * 时间:2018/8/10
 * qq(872179966)
 */

public class PlayDraftsFragment extends BaseFragment {
    @BindView(R.id.rv_rv)
    MyRecycleView rvRv;
    private PlayDraftsAdapter mAdapter;
    private ContentDialog contentDialog;
    private List<DBPlayBean> play;

    @Override
    protected void initView() {
        contentDialog = new ContentDialog(getContext());
        mAdapter = new PlayDraftsAdapter();
        rvRv.setLayoutManager(new LinearLayoutManager(getContext()));
        rvRv.setAdapter(mAdapter);
        rvRv.isCanRefresh(false);
        play = DBUtils.selectPlayDB("play");
        mAdapter.addData(play);
        ((DraftsAcitvity)getActivity()).setPlayNumber(play.size()+"");
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DBPlayBean dbBean = play.get(position);
                Intent intent = new Intent(getContext(), CommonActivity.class);
                intent.putExtra(CommonActivity.FRAGMENT_KEY,CommonActivity.KEY_ADD_PLAY);
                intent.putExtra("dbBean",dbBean);
                startActivity(intent);
            }
        });
    }

    public void refreshData() {
        play = DBUtils.selectPlayDB("play");
        mAdapter.getData().clear();
        mAdapter.addData(play);
        mAdapter.notifyDataSetChanged();
        ((DraftsAcitvity)getActivity()).setPlayNumber(play.size()+"");

    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_play_drafts;
    }

    private class PlayDraftsAdapter extends BaseQuickAdapter<DBPlayBean, BaseViewHolder> {
        public PlayDraftsAdapter() {
            super(R.layout.item_play_drafts_adapter);
        }

        @Override
        protected void convert(BaseViewHolder helper, DBPlayBean item) {
            TextView mContent = helper.getView(R.id.tv_content);
            TextView mTvTime = helper.getView(R.id.tv_time);
            mContent.setText(item.getPath());
            mTvTime.setText(TimeUtils.formatDateTime(item.getTime()));

            PressImageView deletePlay = helper.getView(R.id.iv_delete_play);
            deletePlay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    contentDialog.setContent("确定删除草稿?").setAffirmContent("删除").setAffirmContentColor(R.color.red_bag_color).setCancelContent("取消").setOnTwoClickListener(new ContentDialog.OnTwoClickListener() {
                        @Override
                        public void onAffirm() {
                            if (DBUtils.deletePlayDB(item.getPath())) {
                                showToast("删除成功!");
                                refreshData();
                            }else {
                                showToast("删除失败!");
                            }

                        }

                        @Override
                        public void onCancel() {

                        }
                    }).show();
                }
            });
        }
    }
}
